{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(__doc__)\n",
    "%matplotlib inline\n",
    "\"\"\"\n",
    ":copyright: 2017 H2O.ai, Inc.\n",
    ":license:   Apache License Version 2.0 (see LICENSE for details)\n",
    "\"\"\"\n",
    "import matplotlib\n",
    "from h2o4gpu import DAAL_SUPPORTED\n",
    "from sklearn import datasets, linear_model\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "diabetes = datasets.load_diabetes()\n",
    "# Use only one feature\n",
    "diabetes_X = diabetes.data[:, np.newaxis, 2]\n",
    "\n",
    "# Split the data into training/testing sets\n",
    "diabetes_X_train = diabetes_X[:-20]\n",
    "diabetes_X_test = diabetes_X[-20:]\n",
    "\n",
    "# Split the targets into training/testing sets\n",
    "diabetes_y_train = diabetes.target[:-20]\n",
    "diabetes_y_test = diabetes.target[-20:]\n",
    "\n",
    "if DAAL_SUPPORTED:\n",
    "    from h2o4gpu.solvers.daal_solver.daal_data import getNumpyShape\n",
    "    import h2o4gpu\n",
    "\n",
    "    lin_solver_daal = h2o4gpu.LinearRegression(fit_intercept=True,\n",
    "                                               verbose=True,\n",
    "                                               backend='daal',\n",
    "                                               method=h2o4gpu.LinearMethod.normal_equation)\n",
    "\n",
    "    rows, cols = getNumpyShape(diabetes_y_train)\n",
    "    y = diabetes_y_train.reshape(cols, rows)\n",
    "    lin_solver_daal.fit(diabetes_X_train, y)\n",
    "    daal_predicted = lin_solver_daal.predict(diabetes_X_test)\n",
    "\n",
    "    plt.scatter(diabetes_X_test, diabetes_y_test,  color='black')\n",
    "    plt.plot(diabetes_X_test, daal_predicted, color='red', linewidth=3)\n",
    "    plt.xticks(())\n",
    "    plt.yticks(())\n",
    "    plt.show()\n",
    "else:\n",
    "    \n",
    "    from sklearn.metrics import mean_squared_error, r2_score\n",
    "\n",
    "    # Create linear regression object\n",
    "    regr = linear_model.LinearRegression()\n",
    "    \n",
    "    # Train the model using the training sets\n",
    "    regr.fit(diabetes_X_train, diabetes_y_train)\n",
    "    \n",
    "    # Make predictions using the testing set\n",
    "    diabetes_y_pred = regr.predict(diabetes_X_test)\n",
    "    \n",
    "    # The coefficients\n",
    "    print('Coefficients: \\n', regr.coef_)\n",
    "    # The mean squared error\n",
    "    print(\"Mean squared error: %.2f\"\n",
    "          % mean_squared_error(diabetes_y_test, diabetes_y_pred))\n",
    "    # Explained variance score: 1 is perfect prediction\n",
    "    print('Variance score: %.2f' % r2_score(diabetes_y_test, diabetes_y_pred))\n",
    "    \n",
    "    # Plot outputs\n",
    "    plt.scatter(diabetes_X_test, diabetes_y_test,  color='black')\n",
    "    plt.plot(diabetes_X_test, diabetes_y_pred, color='blue', linewidth=3)\n",
    "    plt.xticks(())\n",
    "    plt.yticks(())\n",
    "    plt.show()\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
